package com.sz.biz.trade.ord.service.impl;

import com.sz.biz.trade.constants.TrdErrorCodes;
import com.sz.biz.trade.ord.entity.TrdOrderGoodsRel;
import com.sz.biz.common.AbstractVersionEntityService;
import com.sz.biz.trade.ord.service.TrdOrderGoodsRelService;
import com.sz.biz.trade.ord.dto.TrdOrderGoodsRelDto;
import com.sz.biz.trade.ord.dto.TrdOrderGoodsRelShortDto;
import com.sz.biz.trade.ord.dto.TrdOrderGoodsTypeShortDto;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.exception.Exceptions;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Function: 贸易订单服务<br>
 * Author: jifeixing <br>
 * Date: 2017-07-15 14:10:00
 */
@Service("trdOrderGoodsRelService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TrdOrderGoodsRelServiceImpl extends AbstractVersionEntityService implements TrdOrderGoodsRelService {


    @Override
    protected String getMapperNamespace() {
        return "TrdOrderGoodsRelMapper";
    }
    /**
     * 订单商品
     * @param trdOrderGoodsRel
     */
    @Override
    public void save(TrdOrderGoodsRel trdOrderGoodsRel) {
        try {
            dao.save(getSqlName("insert"), trdOrderGoodsRel);
        }catch (DataIntegrityViolationException e){
            throw Exceptions.bizException(TrdErrorCodes.ERROR_ORDER_AMOUNT_TOO_LARGE);
        }
    }
    /**
     * 订单下商品数量
     * @param orderNo
     * @return
     */
    @Override
    public List<TrdOrderGoodsRelShortDto> findShortByOrderNo(String orderNo, Integer limit) {
        ParamData paramData = new ParamData();
        paramData.put("orderNo", orderNo);
        paramData.put("limit", limit);
        return dao.findForList(getSqlName("findShortByOrderNo"), paramData, TrdOrderGoodsRelShortDto.class);
    }

    /**
     * 订单下所有商品
     * @param orderNo
     * @return
     */
    @Override
    public List<TrdOrderGoodsRelDto> findByOrderNo(String orderNo, Integer type) {
        ParamData paramData = new ParamData();
        paramData.put("orderNo", orderNo);
        paramData.put("type", type);
        return dao.findForList(getSqlName("findByOrderNo"), paramData, TrdOrderGoodsRelDto.class);
    }
    /**
     * 根据订单号删除
     * @param orderNo
     */
    @Override
    public void deleteByOrderNo(String orderNo, Integer type) {
        ParamData paramData = new ParamData();
        paramData.put("orderNo", orderNo);
        paramData.put("type", type);
        dao.delete(getSqlName("deleteByOrderNo"), paramData);
    }
    /**
     * 根据订单号删除(修改状态为删除状态)
     * @param orderNo
     */
    @Override
    public void updateToDelete(String orderNo, Integer type){
        ParamData paramData = new ParamData();
        paramData.put("orderNo", orderNo);
        paramData.put("type", type);
        dao.update(getSqlName("updateToDelete"), paramData);
    }
    /**
     * 订单下所有商品类别
     * @param orderNo
     * @param limit
     * @return
     */
    @Override
    public List<TrdOrderGoodsTypeShortDto> findGoodsTypeShortByOrderNo(String orderNo, Integer limit) {
        ParamData paramData = new ParamData();
        paramData.put("orderNo", orderNo);
        paramData.put("limit", limit);
        return dao.findForList(getSqlName("findGoodsTypeShortByOrderNo"), paramData, TrdOrderGoodsTypeShortDto.class);
    }
}
